package com.weitu.environmental.monitor.controller;


import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.environmental.monitor.entity.MonitorPoint;
import com.weitu.environmental.monitor.entity.MonitoringInstrument;
import com.weitu.environmental.monitor.service.MonitoringInstrumentService;
import com.weitu.paging.DataGridPagination;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangqingxi 2017-03-31
 */
@Controller
@RequestMapping("environmental/monitor/instrument")
public class MonitoringInstrumentController extends BaseController {

    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "environmental/monitor/instrument";

    private final MonitoringInstrumentService monitoringInstrumentService;

    @Autowired
    public MonitoringInstrumentController(MonitoringInstrumentService monitoringInstrumentService) {
        this.monitoringInstrumentService = monitoringInstrumentService;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView listView() {

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.setViewName(MODULE_BATH_PATH + "/list");

        return modelAndView;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView addView() {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(MODULE_BATH_PATH + "/add");
        return modelAndView;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public ModelAndView editView(String id) {

        MonitoringInstrument monitoringInstrument = monitoringInstrumentService.findById(id);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(MODULE_BATH_PATH + "/edit");
        modelAndView.addObject("instrument", monitoringInstrument);

        return modelAndView;
    }

    @RequestMapping(value = "/details", method = RequestMethod.GET)
    public ModelAndView detailsView(String id) {

        MonitoringInstrument monitoringInstrument = monitoringInstrumentService.findById(id);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(MODULE_BATH_PATH + "/details");
        modelAndView.addObject("instrument", monitoringInstrument);

        return modelAndView;
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/findById")
    public MonitoringInstrument findById(String id) {
        return monitoringInstrumentService.findById(id);
    }

    @ResponseBody
    @RequestMapping("/findByParam")
    public Object findByParam(HttpServletRequest request) {


        Map<String, Object> paramMap = new HashMap<String, Object>();

        //获取前台参数
        putParametersIntoMap(paramMap);

        Integer totalNum = monitoringInstrumentService.findByParamCount(paramMap);//获取总数
        DataGridPagination pagination = DataGridPagination.getInstance(request, totalNum);

        PageView pageView = pagination.getPageView();//生成pageView分页对象
        paramMap.put("pageView", pageView);

        List<MonitoringInstrument> monitoringInstrumentList = monitoringInstrumentService.findByParam(paramMap);

        return pagination.flushDataGridData(monitoringInstrumentList);
    }


    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @SystemLog(module = "环保管理_基本信息管理_监测点管理", methods = "添加监测点")
    public AjaxResult add() {


        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取前台参数
        getAddBasicFieldMap(paramMap);

        Integer result = monitoringInstrumentService.add(paramMap);

        return resultAjaxResult(result);
    }

    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @SystemLog(module = "环保管理_基本信息管理_监测点管理", methods = "编辑监测点")
    public AjaxResult edit() {

        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取前台参数
        getEditBasicFieldMap(paramMap);

        Integer result = monitoringInstrumentService.updateById(paramMap);

        return resultAjaxResult(result);
    }

    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @SystemLog(module = "环保管理_基本信息管理_监测点管理", methods = "删除监测点")
    public AjaxResult delete() {

        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取前台参数
        getEditBasicFieldMap(paramMap);

        Integer result = monitoringInstrumentService.updateById(paramMap);

        return resultAjaxResult(result);
    }


    /**
     * 处理返回参数
     *
     * @param result 返回值
     * @return 返回值
     */
    private AjaxResult resultAjaxResult(Integer result) {
        AjaxResult ajaxResult = new AjaxResult();
        if (result > 0) {
            ajaxResult.setSuccess(true);
            ajaxResult.setMessage("操作成功");
        }
        return ajaxResult;
    }


}
